//
//  SaveTool.m
//  UI_豆瓣
//
//  Created by WangZe on 15/10/13.
//  Copyright © 2015年 王泽龙. All rights reserved.
//

#import "SaveTool.h"

@implementation SaveTool
+ (void)isHaveFolder
{
//    // 找到沙盒路径
//    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES) lastObject];
//    // 凭借电影的文件夹
//    NSString *movieFile = [sandBoxPath stringByAppendingPathComponent:@"Movie"];
//    // 创建一个文件管理者
//    NSFileManager *manager = [NSFileManager defaultManager];
//    if (![manager fileExistsAtPath:movieFile]) {
//        [manager createDirectoryAtPath:movieFile withIntermediateDirectories:YES attributes:nil error:nil];
//    }
 
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *movieFile = [sandBoxPath stringByAppendingPathComponent:@"Movie"];
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:movieFile]) {
        [manager createDirectoryAtPath:movieFile withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

+ (BOOL)isHaveMovieInPlist:(MovieList *)movie
{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Movie/movie.plist"];
    NSLog(@"%@", docPath);
    NSArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    for (MovieList *tempMovie in movieArr) {
        if ([tempMovie.movieId isEqualToString:movie.movieId]) {
            return NO;
        }
    }
    return YES;
}

+ (void)saveMovieToPlist:(MovieList *)movie
{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Movie/movie.plist"];
    NSMutableArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    if (movieArr == nil) {
        movieArr = [NSMutableArray array];
        
    }
    [movieArr addObject:movie];
    
    // 归档
    NSLog(@"~~~~~~~~~~~%@", docPath);
    [NSKeyedArchiver archiveRootObject:movieArr toFile:docPath];
    
}

+ (void)cancleMovieInPlist:(MovieList *)movie
{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Movie/movie.plist"];
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    for (MovieList *tempMovie in arr) {
        if ([tempMovie.movieId isEqualToString:movie.movieId]) {
            [arr removeObject:tempMovie];
            break;
        }
    }
    [NSKeyedArchiver archiveRootObject:arr toFile:docPath];
}
@end
